#!/bin/bash
#
# Handle the main menu for building MaxWit Linux
#
# http://www.maxwit.com
# http://maxwit.googlecode.com
#
# Authors:
#     Conke Hu    <conke.hu@gmail.com>
#     Tiger Yu    <tigerflying.yu@gmail.com>
#     Fleya Hou   <fleya.hou@gmail.com>
#


KERNEL_TOP="${MW_TOP_DIR}/kernel"


BuildKernel()
{
	KERN_OPT="ARCH=arm CROSS_COMPILE=${TARGET_PLAT}- INSTALL_MOD_PATH=${ROOTFS_PATH}"

	case "${TARGET_SOC}" in
	*)
		TARGET_MACH=realview
		;;
	esac

	mkdir -vp ${IMAGES_PATH}

	Unpack ${MWP_KERNEL} kernel

	cd ${TARGET_BUILD_PATH}/${MWP_KERNEL}

	if [ -e ${KERNEL_TOP}/${TARGET_MACH}_defconfig ]; then
		cp -v ${KERNEL_TOP}/${TARGET_MACH}_defconfig arch/${TARGET_ARCH}/configs/
	elif [ -e arch/${TARGET_ARCH}/configs/${TARGET_MACH}_defconfig ]; then
		sed -i -e 's/# CONFIG_AEABI.*/CONFIG_AEABI=y/' \
			-e '/CONFIG_OABI_COMPAT/d' \
			-e '/CONFIG_AEABI/a\# CONFIG_OABI_COMPAT is not set/' \
			-e 's/\(CONFIG_SYSFS_DEPRECATED.*\)=y/# \1 is not set/' \
			arch/${TARGET_ARCH}/configs/${TARGET_MACH}_defconfig
	else
		echo "${TARGET_MACH}: no corresponding kernel configuration found!"
		exit 1
	fi

	make ${KERN_OPT} ${TARGET_MACH}_defconfig

	make ${KERN_OPT} ${BUILD_JOBS} && \
	make ${KERN_OPT} modules_install ||  exit 1
	echo

	case "${TARGET_ARCH}" in
	x86 | i386 | x86_64)
		cp -v ${PWD}/arch/${TARGET_ARCH}/boot/bzImage ${IMAGES_PATH}/bzImage.${TARGET_MACH}
		cp -v ${IMAGES_PATH}/bzImage.${TARGET_MACH} ${TFTP_PATH}/bzImage
		;;

	mips*)
		cp vmlinux ${IMAGES_PATH}/vmlinux.${TARGET_MACH}
		cp -v ${IMAGES_PATH}/vmlinux.${TARGET_MACH} ${TFTP_PATH}/vmlinux
		;;

	*)
		cp arch/${TARGET_ARCH}/boot/zImage ${IMAGES_PATH}/zImage.${TARGET_MACH}
		cp -v ${IMAGES_PATH}/zImage.${TARGET_MACH} ${TFTP_PATH}/zImage
		;;
	esac
}

